package com.custom.action.core.methods.table;

import com.custom.action.core.methods.AbstractMethod;
import com.custom.action.core.methods.MethodKind;
import com.custom.comm.utils.AssertUtil;
import com.custom.jdbc.executebody.BaseSqlExecutorBody;
import com.custom.jdbc.executebody.ExecuteBodyHelper;
import com.custom.jdbc.interfaces.CustomSqlSession;
import com.custom.jdbc.session.JdbcSqlSessionFactory;

/**
 * @author XiaoBai
 * @since 2024/4/18 15:28
 */
public class ExecTableSql extends AbstractMethod {
    @Override
    protected <T> CustomSqlSession createSqlSession(JdbcSqlSessionFactory sqlSessionFactory, Class<T> target, Object[] params) throws Exception {
        AssertUtil.checkNotEmpty(params[0], "execute sql cannot be empty.");
        BaseSqlExecutorBody executorBody = ExecuteBodyHelper.createExecAnySql(params[0].toString(), sqlPrintSupport);
        return sqlSessionFactory.createSqlSession(executorBody);
    }

    @Override
    protected <T> Object doExecute(JdbcSqlSessionFactory sqlSessionFactory, Class<T> target, Object[] params) throws Exception {
        CustomSqlSession sqlSession = createSqlSession(sqlSessionFactory, target, params);
        sqlSessionFactory.getJdbcExecutor().execTableInfo(sqlSession);
        return null;
    }

    @Override
    public MethodKind getKind() {
        return MethodKind.EXEC_TABLE_SQL;
    }
}
